//
//  WmcCircularProgressLayer.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/7/7.
//

import UIKit

class WmcCircularProgressLayer: CAShapeLayer {
    // Lifecycle

    override init() {
        super.init()
        config()
    }

    override init(layer: Any) {
        super.init(layer: layer)
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // Internal

    override func layoutSublayers() {
        super.layoutSublayers()
    }

    func config() {
        backgroundColor = UIColor.clear.cgColor
        fillColor = UIColor.clear.cgColor
        strokeColor = UIColor.wmc_red_F54A45.cgColor
        lineWidth = 4
        lineCap = .round
    }

    func setProgress(_ progress: CGFloat) {
        let startAngle = -(CGFloat.pi / 2)
        let progressAnlge = (CGFloat.pi * 2) * progress
        let endAngle = progressAnlge + startAngle
        let center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2)
        let bezierPath = UIBezierPath(arcCenter: center,
                                      radius: (bounds.size.width - lineWidth) / 2,
                                      startAngle: startAngle,
                                      endAngle: endAngle,
                                      clockwise: true)
        path = bezierPath.cgPath
    }
}
